<?php

namespace App\Http\Controllers\MiniAPP;

use App\Enums\MemberBalanceType;
use App\Enums\OrderFrom;
use App\Enums\OrderType;
use App\Enums\PayChannel;
use App\Enums\TradeStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Requests\MiniAPP\MemberRechargeRequest;
use App\LeShua\LeShua;
use App\Models\MiniappMemberRechargeOption;
use App\Models\OnlineTradeOrder;
use App\Models\Order;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    public function test(Request $request)
    {
        $onlineStore = Store::getOnlineStore();

        $payment = new LeShua($onlineStore->leshua_num);

        $res = $payment->miniPay($request->openid, 'TEST'.time(), 1);

        return ApiResponseHelper::json($res->getJSPay());
    }

    public function checkOrder(OnlineTradeOrder $order)
    {
        if ($order->status != TradeStatus::Payed) {
            return ApiResponseHelper::json([], 400, '订单'.$order->status->label());
        }

        return ApiResponseHelper::json([]);
    }

    public function memberRecharge(MemberRechargeRequest $request)
    {
        try {
            DB::transaction(function () use ($request, &$leshuaRes, &$tradeOrder) {

                $rechargeOption = MiniappMemberRechargeOption::findOrFail($request->option_id);
                $store = Store::findOrFail($request->store_id);

                if (empty($store->leshua_num)) {
                    throw new \Exception('商家未配置乐刷商户号');
                }

                //先创建充值订单
                /**
                 * @var Order $order
                 */
                $order = Order::create([
                    'order_no' => Order::generateOrderNo(),
                    'user_id' => 0,
                    'member_id' => Auth::id(),
                    'store_id' => $store->id,
                    'status' => TradeStatus::Created,
                    'money' => $rechargeOption->money,
                    'pay_channel' => PayChannel::LeShua,
                    'comment' => '会员储值',
                    'type' => OrderType::MemberRecharge,
                    'from' => OrderFrom::MiniAPP,
                    'give_money' => 0,
                    'origin_money' => $rechargeOption->money,
                    'member_balance_type' => MemberBalanceType::General,
                    'give_table_money' => $rechargeOption->give_table_money,
                    'miniapp_member_recharge_option_id' => $rechargeOption->id,
                ]);

                /**
                * @var OnlineTradeOrder $tradeOrder
                */
                $tradeOrder = OnlineTradeOrder::create([
                    'order_no' => OnlineTradeOrder::genOrderNo(),
                    'order_ids' => [$order->id],
                    'total_money' => $order->money,
                    'status' => TradeStatus::Created,
                    'store_id' => $store->id,
                ]);

                $payment = new LeShua($store->leshua_num);
                $leshuaRes = $payment->miniPay($request->openid, $tradeOrder->order_no, $tradeOrder->total_money * 100);
            });
        } catch (\Exception $exception) {
            return ApiResponseHelper::json([], 400, $exception->getMessage());
        }

        return ApiResponseHelper::json([
            'jsapi' => $leshuaRes->getJSPay(),
            'order_no' => $tradeOrder->order_no,
        ]);
    }
}
